﻿(function () {

    // tip

    // local variables
    var _zQuery;

    // make sure $ is defined
    if (!window.$) {
        console.error("this lib is depend on jQuery, the $ must be defined!");
        return undefined;
    }

    // make sure Z is not defined
    if (window.Z || window.zQuery) {
        console.error("this lib is not avaliable, because variable Z is defined!");
        return undefined;
    }    

    //begin: constructor and basic extend method
    _zQuery = window.Z = window.zQuery = function (obj) {
        return new _zQuery.prototype.init(obj);
    };

    _zQuery.prototype = {
        init: function (obj) {         
            this[0] = obj;

            //abondon
            //var t_trycalljmethod = function (that, method, dompara) {
            //    if ((that[0] instanceof jQuery) && that[0][method]) {
            //        that[0][method].call(dompara);
            //    } else {
            //        console.error("the inner object of the zQuery object is not a jQuery object");
            //    }
            //};
                        
            // this extend is adding on init
            this.extend = this.constructor.prototype.extend = function (json) {
                for (var idx in json) {                    
                    this.constructor.prototype[idx] = json[idx];
                }
            };

            this.appendNewChild = this.constructor.prototype.appendNewChild = function (jquerydom) {
                if ((this[0] instanceof jQuery) && this[0].append) {
                    this[0].append(jquerydom);
                } else {
                    console.error("the inner object of the zQuery object is not a jQuery object");
                }
                //t_trycalljmethod(this, "append", jquerydom);
            };

            this.appendAfterSelf = this.constructor.prototype.appendAfterSelf = function (jquerydom) {
                if ((this[0] instanceof jQuery) && this[0].after) {
                    this[0].after(jquerydom);
                } else {
                    console.error("the inner object of the zQuery object is not a jQuery object");
                }
                //t_trycalljmethod(this, "after", jquerydom);
            };

            this.appendBeforeSelf = this.constructor.prototype.appendBeforeSelf = function (jquerydom) {
                if ((this[0] instanceof jQuery) && this[0].before) {
                    this[0].before(jquerydom);
                } else {
                    console.error("the inner object of the zQuery object is not a jQuery object");
                }
                //t_trycalljmethod(this, "before", jquerydom);
            };

            return this;
        }
    };

    // this extend is adding on the function named _zQuery
    _zQuery.extend = _zQuery.prototype.extend = function (json) {
        for (var idx in json) {
            this[idx] = json[idx];
        }
    }

    //end: //constructor and basic extend method

    //begin: oop area

    _zQuery.oop_parseJSON = function (strjson) {
        return $.parseJSON(strjson);
    }

    _zQuery.oop_parseString = function (json) {
        return JSON.stringify(json);
    }

    //copyfrom http://www.cnblogs.com/loogn/archive/2011/06/20/2085165.html
    //var template1 = "我是{0}，今年{1}了";
    //var template2 = "我是{name}，今年{age}了";
    //var result1 = template1.format("loogn", 22);
    //var result2 = template2.format({ name: "loogn", age: 22 });
    _zQuery.str_fmt = function (str, args) {
        var result = str;
        if (arguments.length > 0) {
            if (arguments.length == 2 && typeof (args) == "object") {
                for (var key in args) {
                    if (args[key] != undefined) {
                        var reg = new RegExp("({" + key + "})", "g");
                        result = result.replace(reg, args[key]);
                    }
                }
            }
            else {
                for (var i = 1; i < arguments.length; i++) {
                    if (arguments[i] != undefined) {
                        //var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题，谢谢何以笙箫的指出
                        var reg = new RegExp("({)" + (i - 1) + "(})", "g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
        }
        return result;
    }

    //end: //oop area

    //begin: dom area
    _zQuery.dom_create = function (domstr, attrjson) {
        return $(domstr).attr(attrjson);
    }

    _zQuery.dom_makez = function (idorjquerydomordom) {
        if (typeof idorjquerydomordom == "string") {
            // parameter is only a string
            if (!idorjquerydomordom[0]) {
                console.error("parameter has no length");
                return;
            }
            if (idorjquerydomordom[0] == "#") {
                return Z($(idorjquerydomordom));
            } else {
                return Z($("#" + idorjquerydomordom));
            }
            
        } else if (idorjquerydomordom instanceof jQuery) {
            // parameter is a jQuery object
            return Z(idorjquerydomordom);
        } else if (idorjquerydomordom instanceof Element) {
            return Z($(idorjquerydomordom));
        } else {
            console.error("parameter of idorjquerydomordom is err!");
        }
    }

    //begin: control area

    _zQuery.dom_sel_addopt = function (idorjquerydomordom, arr, gettextcb) {
        if (typeof gettextcb != "function") {
            console.error("gettextcb is not a function!");
        }
        var t_zsel = Z.dom_makez(idorjquerydomordom);
        var t_i = 0;
        var t_thistext = "";
        var t_opt;

        // add options one by one
        for (t_i = 0; t_i < arr.length; t_i++) {

            // getcurrent text
            t_thistext = gettextcb(arr[t_i]);

            t_opt = Z.dom_create("<option></option>", arr[t_i]);

            // set cur option's text
            t_opt.html(t_thistext);
        
            // append to select
            t_zsel.appendNewChild(t_opt);
            
        }
    }

    // clear a select's option
    _zQuery.dom_sel_emp = function (idorjquerydomordom) {
        var t_zobj = _zQuery.dom_makez(idorjquerydomordom);

        // clear all options of select
        t_zobj[0].html("");
        
    }

    _zQuery.dom_sel_empwithtext = function (idorjquerydomordom, placeholder, placeholderval) {
        var t_zobj = _zQuery.dom_makez(idorjquerydomordom);

        // clear all options of select
        t_zobj[0].html("");

        // add option text
        var t_jtext = _zQuery.dom_create("<option></option>", { "value": placeholderval });
    
        t_jtext.html(placeholder);

        t_zobj.appendNewChild(t_jtext);
        
    }

    //end: //control area

    //end: //dom area

    //begin: web area

    // ajax post sync
    _zQuery.web_ajaxPostSync = function (url, data, successcallback, completecallback) {

        return $.ajax({

            url: url,
            type: "POST",
            data: data,
            dataType: "json",
            async: false,
            success: successcallback,
            complete: completecallback,
        });
    };

    // ajax post async
    _zQuery.web_ajaxPostAsync = function (url, data, successcallback, completecallback) {

        return $.ajax({

            url: url,
            type: "POST",
            data: data,
            dataType: "json",
            async: true,
            success: successcallback,
            complete: completecallback,
        });
    };

    //end: //web area



})();